package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.Classes.Model.Request.GetindustypReq;
import com.ydkj.a37e_mall.Classes.Model.Response.FindShopTypeResp;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_get;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.GsonImpl;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by Administrator on 2017/3/1.
 */

public class StoreTypeActivity extends BaseActivity {
    private Activity mActivity;
    private ImageView back;
    private ListView lv_type;
    private MyApplication myApplication;
    private List<FindShopTypeResp.ResultEntity.DataEntity> typeList;
    private StoreTypeAdapter storeTypeAdapter;
    private int code;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_type);
        mActivity = this;
        init();
    }

    private void init() {
        if (getIntent().getExtras() != null) {
            code = getIntent().getExtras().getInt("code");
            Log.e("ssss", "门店类型选择===>" + code);
        }


        myApplication = MyApplication.getInstance();
        back = (ImageView) mActivity.findViewById(R.id.img_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Getindustyp();

        typeList = new ArrayList<>();
        lv_type = (ListView) mActivity.findViewById(R.id.lv_type);
        storeTypeAdapter = new StoreTypeAdapter(typeList);
        lv_type.setAdapter(storeTypeAdapter);
    }

    //获取行业列表
    private void Getindustyp() {
        CustomProgressDialog.createDialog(mActivity).show();
        GetindustypReq req = new GetindustypReq();
        req.setUuid(myApplication.getDataEntity().getUuid());
        req.setType("0");
        req.setPage("1");
        req.setPage_size("10");
        RequestParams params = new RequestParams();
        params.put("secret", req.getSecret());
        Async_http_get.get(Const.COMMON_ADDRESS + req.urlString(), params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                Log.e("url", "type_json==" + response.toString());
                if (errno.equals("000")) {
                    FindShopTypeResp resp = GsonImpl.get().toObject(response.toString(), FindShopTypeResp.class);
                    typeList.addAll(resp.getResult().getData());
                    storeTypeAdapter.notifyDataSetChanged();
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    private class StoreTypeAdapter extends BaseAdapter {
        private List<FindShopTypeResp.ResultEntity.DataEntity> typeList;

        private StoreTypeAdapter(List<FindShopTypeResp.ResultEntity.DataEntity> infos) {
            if (infos == null) {
                infos = new ArrayList<FindShopTypeResp.ResultEntity.DataEntity>();
            }
            typeList = infos;
        }

        @Override
        public int getCount() {
            return typeList.size();
        }

        @Override
        public FindShopTypeResp.ResultEntity.DataEntity getItem(int position) {
            return typeList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup viewGroup) {
            ViewHolder vh = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.choose_address_listitem, null);
                vh = new ViewHolder();
                vh.tv_address = (TextView) convertView.findViewById(R.id.bankname);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            convertView.setTag(vh);

            vh.tv_address.setText(getItem(position).getName());
            vh.tv_address.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (code == 1) {
                        Log.e("ssss", "门店类型1111==>" + getItem(position).getName());

                        Intent intent = new Intent(StoreTypeActivity.this, ApplyBusinessActivity.class);
                        intent.putExtra("type", getItem(position).getName());
                        intent.putExtra("type_id", getItem(position).getId());
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                    } else {
                        Intent intent = new Intent(StoreTypeActivity.this, StoreManagementActivity.class);
                        intent.putExtra("type", getItem(position).getName());
                        intent.putExtra("type_id", getItem(position).getId());
                        setResult(Activity.RESULT_OK, intent);
                        finish();

                    }
                }
            });
            return convertView;
        }

        private class ViewHolder {
            public TextView tv_address;
        }
    }
}
